﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BooksOnline.Models;
using BooksOnline.Serivces;

namespace BooksOnline.Controllers
{
    public class BooksController : Controller
    {
        //
        // GET: /Books/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ListAllBooks()
        {
            var booksModel = ModelFactory.GetBooksModel();
            IEnumerable<Book> books = booksModel.Books;
                
            ViewData.Model = books;

            return View();
        }

        public PartialViewResult ListMyBooks()
        {
            var booksModel = ModelFactory.GetBooksModel();
            var userId = Convert.ToInt32(System.Web.HttpContext.Current.Session["UserId"]);
            IEnumerable<UserFavouriteBooks> userFavouriteBooks =
                from books in booksModel.UserFavouriteBooks
                where books.User.Id == userId
                select books;

            ViewData.Model = userFavouriteBooks;

            return PartialView();
        }

        //
        // GET: /Books/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Books/Create

        public ActionResult Create()
        {

            return View();
        } 

        //
        // POST: /Books/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                var booksModel = ModelFactory.GetBooksModel();
                var book = new Book()
                               {
                                   Author = collection["Author"],
                                   ISBN = collection["ISBN"],
                                   Name = collection["Name"],
                                   Price = collection["Price"]
                               };
                booksModel.AddToBooks(book);

                UnitOfWork.CurrentInstance.Commit();

                return RedirectToAction("ListAllBooks");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Books/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Books/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Books/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Books/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
